package de.codecasa.gefahrenanalyse;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.prefs.Preferences;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.JButton;
import java.awt.event.ActionListener;


public class MainWindow extends JFrame  implements ActionListener{

	//private static Preferences prefs = Preferences.userRoot().node( "/de/codecasa/gefahrenanalyse" );
	
	private static final long serialVersionUID = 1L;
	private static ResultSet rs;
	private static DB_Manager db;
		
	private JPanel contentPane;
	private static Einstellungen pref = new Einstellungen(); 
	
	
	private JMenuItem mntmDatenbank;
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		//getPreference();		
		
        
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					
					MainWindow frame = new MainWindow();
					frame.setVisible(true);

      				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 */
	public MainWindow() throws SQLException 
	{
		
		setTitle("Gefahrenanalyse 1.0");
		setIconImage(Toolkit.getDefaultToolkit().getImage("/home/af/Downloads/DVGW/symbol_absperrventil.gif"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 1280, 980);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnDatei = new JMenu("Datei");
		menuBar.add(mnDatei);
		
		JMenuItem mntmBeenden = new JMenuItem("Beenden");
		mnDatei.add(mntmBeenden);
		
		JMenu mnEinstellungen = new JMenu("Einstellungen");
		menuBar.add(mnEinstellungen);
		
		JMenuItem mntmFenster = new JMenuItem("Fenster");
		mnEinstellungen.add(mntmFenster);
		
		mntmDatenbank = new JMenuItem("Datenbank");
		mntmDatenbank.addActionListener(this);

		mnEinstellungen.add(mntmDatenbank);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		
		JPanel pnlLeft = new JPanel();
		pnlLeft.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout = (FlowLayout) pnlLeft.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setHgap(100);
		pnlLeft.setBackground(Color.WHITE);
		pnlLeft.setBounds(0, 0, 200, 700);
		contentPane.add(pnlLeft, BorderLayout.WEST);
	
		//db = new DB_Manager();
        //rs = db.ReturnQuery("select * from Block;");
	
		
        
        
        JPanel frame = new JPanel();
        

        DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Wurzel" );

        for ( int nodeCnt = 0; nodeCnt < 4; nodeCnt++ )
        {
          DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode( "Knoten " + nodeCnt );
          root.add( dmtn );

          for ( int leafCnt = 1; leafCnt < 4; leafCnt++ )
            dmtn.add( new DefaultMutableTreeNode( "Blatt " + (nodeCnt * 3 + leafCnt) ) );
        }
        frame.add( new JScrollPane( ) );
      
        frame.setVisible( true );
        
                JTree tree = new JTree( root );
                tree.setRootVisible(false);
                pnlLeft.add(tree);
                tree.setVisibleRowCount(50);
                tree.setModel(new DefaultTreeModel(
                	new DefaultMutableTreeNode("root") {
                		{
                			DefaultMutableTreeNode node_1;
                			DefaultMutableTreeNode node_2;
                			DefaultMutableTreeNode node_3;
                			DefaultMutableTreeNode node_4;
                			DefaultMutableTreeNode node_5;
                			node_1 = new DefaultMutableTreeNode("Kunden");
                				node_2 = new DefaultMutableTreeNode("Kunde KID=01");
                					node_2.add(new DefaultMutableTreeNode("Name und Anschrift"));
                					node_3 = new DefaultMutableTreeNode("Gebeb\u00E4ude");
                						node_3.add(new DefaultMutableTreeNode("Sandstr. 10, 41987 K\u00F6ln"));
                						node_4 = new DefaultMutableTreeNode("Weingartenweg 1, 64277 Bickenbach");
                							node_5 = new DefaultMutableTreeNode("GFA 01.02.2014");
                								node_5.add(new DefaultMutableTreeNode("Block 1"));
                								node_5.add(new DefaultMutableTreeNode("RLP 1"));
                								node_5.add(new DefaultMutableTreeNode("Foto 1"));
                								node_5.add(new DefaultMutableTreeNode("Block 2"));
                							node_4.add(node_5);
                							node_5 = new DefaultMutableTreeNode("GFA 17.062015");
                								node_5.add(new DefaultMutableTreeNode("Block 1"));
                								node_5.add(new DefaultMutableTreeNode("Foto1"));
                								node_5.add(new DefaultMutableTreeNode("Block 2"));
                								node_5.add(new DefaultMutableTreeNode("RLP 1"));
                								node_5.add(new DefaultMutableTreeNode("Foto 1"));
                								node_5.add(new DefaultMutableTreeNode("Foto 2"));
                							node_4.add(node_5);
                						node_3.add(node_4);
                					node_2.add(node_3);
                				node_1.add(node_2);
                			add(node_1);
                			node_1 = new DefaultMutableTreeNode("Katalog");
                				node_2 = new DefaultMutableTreeNode("Bl\u00F6cke");
                					node_2.add(new DefaultMutableTreeNode("Block 1"));
                					node_2.add(new DefaultMutableTreeNode("Block 2"));
                				node_1.add(node_2);
                				node_2 = new DefaultMutableTreeNode("DVGW Symbol-Katalog");
                					node_2.add(new DefaultMutableTreeNode("Absperrventil"));
                					node_2.add(new DefaultMutableTreeNode("Eckventil"));
                					node_2.add(new DefaultMutableTreeNode("Filter"));
                				node_1.add(node_2);
                				node_2 = new DefaultMutableTreeNode("Fotos");
                					node_2.add(new DefaultMutableTreeNode("Foto 1"));
                					node_2.add(new DefaultMutableTreeNode("Foto 2"));
                					node_2.add(new DefaultMutableTreeNode("Foto 3"));
                				node_1.add(node_2);
                			add(node_1);
                		}
                	}
                ));
        
        pnlLeft.add(frame);

/*        
        tree.getSelectionModel().addTreeSelectionListener(
          new TreeSelectionListener()
          {
            @Override public void valueChanged( TreeSelectionEvent e )
            {
              TreePath path = e.getNewLeadSelectionPath();
              System.out.println( path );
            }
          } );
        
*/		
		

	
		JPanel pnlMid = new JPanel();
		pnlMid.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		pnlMid.setBackground(Color.WHITE);
		contentPane.add(pnlMid, BorderLayout.CENTER);
		
		JPanel pnlTop = new JPanel();
		pnlTop.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout_1 = (FlowLayout) pnlTop.getLayout();
		flowLayout_1.setVgap(30);
		pnlTop.setBackground(Color.WHITE);
		contentPane.add(pnlTop, BorderLayout.NORTH);
		
		
	}

	

  
	
	private void sDialog()
	{
	
		try {
			frmDatabase dialog = new frmDatabase();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
        if (e.getSource() == mntmDatenbank){
            sDialog();
       }
	}
	
	
	
	/*
	  public void setPreference() {

		    // Now set the values
		    prefs.put("Host", pref.getHost());
		    prefs.putInt("Port", pref.getPort());
		    prefs.put("Database", pref.getDatabase());
	
		  }

	  private static void getPreference() {

		    
		    // Now get the values
		    pref.setHost(prefs.get("Host", "localhost"));
		    pref.setPort(prefs.getInt("Port", 3306));
		    pref.setDatabase(prefs.get("Database", "nvn-db"));
	
		  }
	 */ 
	  
}
